package com.bto.solar.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bto.solar.work.common.R;
import com.bto.solar.work.dto.CreateCompanyReq;
import com.bto.solar.work.entity.Company;
import com.bto.solar.work.service.BtoCompanyService;
import com.bto.solar.work.vo.CompanyVO;
import com.bto.solar.work.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private BtoCompanyService btoCompanyService;

    /**
     * 通过当前登陆的用户获取可查询的公司（用户管理-查询框-公司类型）
     */
    @GetMapping("/query-company")
    public R<List<UserVO.Company>> getCompanyByUserQuery() {
        return R.ok(btoCompanyService.getCompanyByUserQuery());
    }

    /**
     * 分页查询公司信息
     */
    @GetMapping("/infos")
    public R<Page<CompanyVO>> getCompanyPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "types", required = false) List<String> types,
            @RequestParam(value = "order_by", required = false, defaultValue = "true") boolean orderBy
    ) {
        Page<CompanyVO> companyVoPage = btoCompanyService.getCompanyVoPage(currentPage, pageSize, orderBy , types);
        return R.ok(companyVoPage);
    }

    /**
     * 公司列表
     */
    @GetMapping("/list")
    public R<List<Company>> getCompanyList() {
        return R.ok(btoCompanyService.getCompanyList());
    }


    /**
     * 新增公司
     */
    @PostMapping("/add")
    public R<String> addCompany(@Valid @RequestBody CreateCompanyReq createCompanyReq) {
        boolean add = btoCompanyService.addCompany(createCompanyReq);
        if (add) {
            return R.ok(null ,"公司新增成功");
        } else {
            return R.fail("公司新增失败");
        }
    }

    /**
     * 修改公司
     */
    @PutMapping("/update")
    public R<String> updateCompany(@Valid @RequestBody CreateCompanyReq createCompanyReq) {
        boolean update = btoCompanyService.updateCompany(createCompanyReq);
        if (update) {
            return R.ok(null,"公司修改成功");
        } else {
            return R.fail(null,"公司修改失败");
        }
    }

    @DeleteMapping("/delete/{company_id}")
    public R<String> deleteCompany(@PathVariable("company_id") Long companyId) {
        boolean delete = btoCompanyService.deleteCompany(companyId);
        if (delete) {
            return R.ok(null,"公司删除成功");
        } else {
            return R.fail(null,"公司删除失败");
        }
    }





}
